package com.trafficradio.trafficradio.network;

import android.os.Handler;
import android.os.Message;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import com.trafficradio.trafficradio.util.Data;
import com.trafficradio.trafficradio.util.CONSTANT;

import java.io.File;
import java.io.IOException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
 * Created by jwzxg on 2016/7/7.
 */
public class OkHttpClientManager {
    private OkHttpClient client;
    private static final String URLVAR = "http://211.87.229.41/TrafficRadio/";
    private Handler handler;
    public OkHttpClientManager(Handler handler){
        client = new OkHttpClient();
        this.handler = handler;
    }
    public void requestWtihPost(String url,Map<String,String> param,final int what){

        FormBody.Builder builder = new FormBody.Builder();
        Set<Map.Entry<String,String>> entrySet= param.entrySet();
        for (Map.Entry<String, String> entry : entrySet)
            builder.add(entry.getKey(),entry.getValue());
        final Request request = new Request.Builder().url(URLVAR+url).post(builder.build()).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = new Message();
                message.what = CONSTANT.FAILURE;
                message.obj = "远程服务器连接失败";
                handler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call,Response response) throws IOException {
                String result = response.body().string();
                Message message = new Message();
                message.what = what;
                message.obj = result;
                handler.sendMessage(message);
            }
        });
    }
    public void updateFile(File file){
        RequestBody filebody = RequestBody.create(
                MediaType.parse(guessMimeType(file.getPath())),file);
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file",file.getName(),filebody)
                .addFormDataPart("username",Data.getUsername())
                .build();
        Request request = new Request.Builder().url(URLVAR+"updatefile.php")
                .post(requestBody).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = new Message();
                message.what = CONSTANT.FAILURE;
                message.obj = "远程服务器连接失败";
                handler.sendMessage(message);

            }

            @Override
            public void onResponse(Call call,Response response) throws IOException {
                String result = response.body().string();
                Message message = new Message();
                message.what = CONSTANT.UPDATEIMAGE;
                message.obj = result;
                handler.sendMessage(message);
            }
        });
    }
    private String guessMimeType(String path)
    {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentTypeFor = fileNameMap.getContentTypeFor(path);
        if (contentTypeFor == null)
        {
            contentTypeFor = "application/octet-stream";
        }
        return contentTypeFor;
    }
}
